import { handleKubectlCore, navigateToKubectl } from './common/kubectl-handler.js';

// 监听扩展安装事件
chrome.runtime.onInstalled.addListener(function(details) {
    if (details.reason === "install") {
        // 设置默认选项
        const defaultOptions = {
            clusterOptions: ["global"],
            namespaceOptions: ["kube-system", "cpaas-system"],
            kubectlOpenType: 'tab' // 添加默认的打开方式
        };
        
        chrome.storage.local.set(defaultOptions, function() {
            console.log('默认选项已初始化');
        });
    }
});

// 处理快捷键命令
chrome.commands.onCommand.addListener(async (command) => {
    if (command === "open-kubectl") {
        // 获取当前标签页
        const tabs = await chrome.tabs.query({active: true, currentWindow: true});
        const currentTab = tabs[0];
        
        // 使用核心逻辑处理URL
        const result = await handleKubectlCore(currentTab.url);
        if (!result.success) {
            console.error(result.error);
            return;
        }

        try {
            // 获取配置的打开方式
            const { kubectlOpenType = 'tab' } = await chrome.storage.local.get(['kubectlOpenType']);
            
            // 使用统一的跳转逻辑
            await navigateToKubectl(currentTab, result.targetUrl, kubectlOpenType);
        } catch (error) {
            console.error('打开终端失败:', error);
        }
    }
}); 